package queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;


public class Queue {
	public static void main(String[] args) {
		  BlockingQueue<Integer> q=new LinkedBlockingQueue<Integer>(2);
		  Producer p=new Producer(q);
		  Consumer c=new Consumer(q);
		  p.start();
		  c.start();
	}

}
class Consumer extends Thread{
	BlockingQueue<Integer> q;
    public Consumer(BlockingQueue<Integer> q)
    {
    	this.q=q;
    }
	public void run()
	{
	  
	  try {
	    while(true)
	    {	 
			System.out.println(q.take());
		   Thread.sleep(200);
	    }
	  } catch (Exception e) {
			e.printStackTrace();
		}
	}
}
class Producer extends Thread{

	BlockingQueue<Integer> q;
    public Producer(BlockingQueue<Integer> q)
    {
    	this.q=q;
    }
	public void run()
	{
      int i=0;
	  try {
	    while(true)
	    {	 
		q.put(i);
		Thread.sleep(200);
		i++;
	    }
	  } catch (Exception e) {
			e.printStackTrace();
		}
	}
}